const md5 = require('md5');
const axios = require('axios');
const {Q} = require('./../utils/zhihuSecret')
const markdown = require('markdown-js');
const fs = require('fs');
const path = require('path');

module.exports = async function fetch(username, offset = 0) {
    const secret = `101_3_2.0+/api/v4/members/${username}/articles?include=data%5B*%5D.comment_count%2Csuggest_edit%2Cis_normal%2Cthumbnail_extra_info%2Cthumbnail%2Ccan_comment%2Ccomment_permission%2Cadmin_closed_comment%2Ccontent%2Cvoteup_count%2Ccreated%2Cupdated%2Cupvoted_followees%2Cvoting%2Creview_info%2Cis_labeled%2Clabel_info%3Bdata%5B*%5D.vessay_info%3Bdata%5B*%5D.author.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B*%5D.author.vip_info%3B&offset=${offset}&limit=20&sort_by=created+"APBQe41fWxSPTpM-Pdmm4TWK4OsygyLXVnc=|1642509338"`;
    const md5Secret = md5(secret);

    const xZse96 = `2.0_${Q(md5Secret)}`

    const config = {
        method: 'get',
        url: `https://www.zhihu.com/api/v4/members/${username}/articles?include=data%5B*%5D.comment_count%2Csuggest_edit%2Cis_normal%2Cthumbnail_extra_info%2Cthumbnail%2Ccan_comment%2Ccomment_permission%2Cadmin_closed_comment%2Ccontent%2Cvoteup_count%2Ccreated%2Cupdated%2Cupvoted_followees%2Cvoting%2Creview_info%2Cis_labeled%2Clabel_info%3Bdata%5B*%5D.vessay_info%3Bdata%5B*%5D.author.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B*%5D.author.vip_info%3B&offset=${offset}&limit=20&sort_by=created`,
        headers: {
            'authority': 'www.zhihu.com',
            'x-zse-93': '101_3_2.0',
            'x-ab-param': 'qap_question_author=0;qap_question_visitor= 0;zr_slotpaidexp=1;tp_dingyue_video=0;tp_topic_style=0;top_test_4_liguangyi=1;tp_zrec=0;pf_adjust=0;pf_noti_entry_num=0;tp_contents=2;se_ffzx_jushen1=0;zr_expslotpaid=1',
            'x-ab-pb': 'CtgBRwA7AgcHPwA/BqYGuQJBBt0H0wf0CzcMMgVSBdgC9gIRBaMHTwPcB7ULGwA0DAELgAVQA6YEfgb6BmoBdwfWBMwC8wMzBNcCeAeyBTID6QQZBewKUQWbB40EmwtcBnsHtwP0AxUFjAV0ASoDVAdAAVcEiwVDAAsE6wZSCykFngUqBtcLtAraBKsGJweyB+ALtADCBWYHoAMBBoQCNwUxBuQKVgwHDA8L4wWiBk8H2AdgC+cFuQfcC2kBjAQzBaEDeQfPC30ClAZyB4kMogNWBRYGMAbGBlcHEmwAAAAAAAALAAAAAAEAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAIAAAAAGAAAAQAAAAAAAAEAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAACwAAAAAAAAAAAAA=',
            'sec-ch-ua-mobile': '?0',
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36',
            'x-requested-with': 'fetch',
            'x-zse-96': xZse96,
            'sec-ch-ua-platform': '"macOS"',
            'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="97", "Chromium";v="97"',
            'accept': '*/*',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'cors',
            'sec-fetch-dest': 'empty',
            'referer': `https://www.zhihu.com/people/${username}/posts`,
            'accept-language': 'en-US,en;q=0.9',
            'cookie': '_zap=8c9c5b28-d38d-4a1b-95c0-0e294811105d; _xsrf=7e483a91-28c8-4171-9f62-c53fe0f18bca; d_c0="APBQe41fWxSPTpM-Pdmm4TWK4OsygyLXVnc=|1642509338"; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1642509340; gdxidpyhxdE=6OqUftG%2FrfNdD7j8r4qoqncnquc4A%5C18pgIurdbVtJaezMG1l3JXVv9eKKKyM8CttfpOd9El9UBYQ%2FLrjwoL62DS7D1yq6fBxXjleQSwDdQwbsx7yQGWDHVxYjCxNdq7QXnt2umvLwOgixOZxSWwSSRQO38Y%2FPA8DA%5CY3BwM13gCQMhH%3A1642510241825; _9755xjdesxxd_=32; YD00517437729195%3AWM_NI=ro13c25xCGJFN9H%2BGRH%2BOWGXvcSJmPENBqL%2FZs1Ow9s%2F576ZADNoB0TDMmh7k9fTRLdqh1CGcH%2BwXpYlYqFJAw6CQFy3Gg7OcEXXgzNcmdX0cd%2FzT%2FbgQX88o3%2BF0r3Va1o%3D; YD00517437729195%3AWM_NIKE=9ca17ae2e6ffcda170e2e6ee98d580878c9cdacf43f7968ba2c85e839b9e85f53d81a6a3b8e5219ca98faccb2af0fea7c3b92ab2b1ffd4e23af7a88da8c94e8ee788ccc65a8cf581b8c654b6bd9dabf05c87928e92cf259aadffb0f633b1f1fbd8b552839c8290c13c989c838eeb3da9e7aab5d148a8afa8d2c24da198e598db7c9bb49792c54283a9faa4d44ebbed8988b645a19b98b9e6728ebda385db3ca1bce19bd725a2b99fb9bb34adede5a7b37af18aafd2ee37e2a3; YD00517437729195%3AWM_TID=gxw3CI8D4wZARVEQERY%2B%2FGnySKqShQaX; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1642509420; captcha_session_v2="2|1:0|10:1642509420|18:captcha_session_v2|88:aUtqVDRvcFNWcEQza3AvY2Y5aXF5L2h2OWpteWZtcGNnL3VMa2tNU3RPVFNEdzhySG1GazA3TFVnUXBQMWZZLw==|8d65b7a58e049a13246d6c7afcb870fc7ce601386038eaef6b2929b0e187dc15"; KLBRSID=3d7feb8a094c905a519e532f6843365f|1642509463|1642509338; _xsrf=PbsVNFyzQmYcf2ni8gyX1AgHvgkKN1Fh; KLBRSID=4efa8d1879cb42f8c5b48fe9f8d37c16|1642508891|1642508891'
        }
    };
    axios(config)
        .then(async function (response) {
            const mdSTR = response.data.data.reduce((acc, item) => {
                acc += `[${item.title}](${item.url}) comments: ${item.comment_count} prase:${item.voteup_count} \r\n`
                return acc;
            }, '')
            fs.appendFileSync(path.join(__dirname, `${username}.md`), mdSTR)
            if (response.data.data.length < 10) {
                process.exit()
            }
            console.log(markdown.makeHtml(mdSTR));
            setTimeout(async () => {
                await fetch(username, offset + 20)
            }, 2000)
        })
        .catch(function (error) {
            console.log(error);
            process.exit()
        });
}
